﻿using System;
using System.Text;
using System.Text.RegularExpressions;

namespace DotNet.Utilities
{
    /// <summary>
    /// 提供常用正则验证
    /// </summary>
    public class RegexUtil
    {
        /// <summary>
        /// 是否为纯数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumber(string str)
        {
            return Regex.IsMatch(str, @"^\d+$");
        }
        /// <summary>
        /// 是否为纯字母
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsLetter(string str)
        {
            return Regex.IsMatch(str, @"^[a-zA-Z]+$");
        }
        /// <summary>
        /// 是否为图片格式
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool IsImage(string filename)
        {
            return Regex.IsMatch(filename, @"(?i)\.(jpg|gif|png|bmp|tiff)$");
        }
        /// <summary>
        /// 验证是否为用户名格式（字母、数字、下划线）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsUsername(string str)
        {
            return Regex.IsMatch(str, @"^\w+$");
        }

        public static bool IsUrl(string str)
        {
            return Regex.IsMatch(str, @"(?i)^http(?s)://([\w\-]+\.)+[\w\-]+(/[\w\-./?%&=]*)?$");
        }

        public static bool IsEmail(string str)
        {
            return Regex.IsMatch(str, @"(?i)^([a-z0-9][a-z0-9_\-\.]+)@([a-z0-9][a-z0-9\.\-]{0,20})\.([a-z]{2,4})$");
        }

        public static bool IsIpAddress(string str)
        {
            return Regex.IsMatch(str, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
        }
        /// <summary>
        /// 验证是否为身份证号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIDCardNumber(string str)
        {
            if (str.Length == 15)
            {
                return Regex.IsMatch(str, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
            }
            else if (str.Length == 18)
            {
                return Regex.IsMatch(str, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$");
            }
            return false;
        }
        /// <summary>
        /// 是否为手机号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsMobile(string str)
        {
            return Regex.IsMatch(str, @"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$");
        }
        /// <summary>
        /// 是否为电话号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsTelphone(string str)
        {
            string patt = @"^((\d{7,8})|(\d{3,4})(-|\s)?(\d{7,8})|(\d{3,4})(-|\s)?(\d{7,8})(-|\s)(\d{1,4})|(\d{7,8})(-|\s)(\d{1,4}))$";
            return Regex.IsMatch(str, patt);
        }
        /// <summary>
        /// 是否为日期格式（不含时间）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDate(string str)
        {
            string patt = @"^(?:(?!0000)[0-9]{4}([-/.\s]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.\s]?)0?2\2(?:29))$";
            return Regex.IsMatch(str, patt);
        }
        /// <summary>
        /// 是否为时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsTime(string str)
        {
            return Regex.IsMatch(str, @"^([01]?[0-9]|2?[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$");
        }
        /// <summary>
        /// 是否为日期时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDateTime(string str)
        {
            string patt = @"^(?:(?!0000)[0-9]{4}([-/.\s]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.\s]?)0?2\2(?:29))\s+([01]?[0-9]|2?[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$";
            return Regex.IsMatch(str, patt); 
        }


    }
}
